package com.elai.analytics.service.impl;
import com.aliyuncs.exceptions.ClientException;
import com.elai.analytics.service.TouchModeService;
import com.elai.baseconfig.domain.ChannelPushConfig;
import com.elai.baseconfig.service.IChannelPushConfigService;
import com.elai.common.core.redis.RedisCache;
import com.elai.common.mode.HuaWeiSendSms;
import com.elai.common.mode.JGUtil;
import com.elai.common.mode.TencentUtil;
import com.elai.third.aliyun.sample.AliyunUtils;
import com.elai.third.aliyun.sample.dto.SendSmsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;


/**
 *发送消息
 */
@Service
public class TouchModeServiceImpl
{

    @Autowired
    private IChannelPushConfigService channelPushConfigService;

    //发送短信方法
    public  Boolean sendMessage(Long id,String telephone) {
        ChannelPushConfig channelPushConfig = channelPushConfigService.selectChannelPushConfigById(id);
        String pushType = channelPushConfig.getPushType();
        pushType="1";
        String appKey = channelPushConfig.getAppKey();
        String appSecret = channelPushConfig.getAppSecret();
        //每次调用生成一次四位数的随机数
        int newcode = (int) (Math.random() * 9999) + 100;
        String code = Integer.toString(newcode);
        int templateId = 0;
        int appid = 1400;
        //阿里云发送短信
        if ("1".equals(pushType)) {
            AliyunUtils aliyunUtil = new AliyunUtils();
         try {
                SendSmsDto sendSmsDto=new SendSmsDto();
                sendSmsDto.setAccessKeyId("LTAI5tHhByERRnAouUDAd42P");
                sendSmsDto.setAccessKeySecret("DbOmnBJpRVpkEUNA6RlIAKsH71oQWY");
                sendSmsDto.setPhoneNumbers(telephone);
                sendSmsDto.setSignName("小象智慧公司官网");
                sendSmsDto.setTemplateCode("SMS_225367805");
                sendSmsDto.setTemplateParam("{\"name\":\"手机\",\"price\":\"1200\"}");
                HashMap hashMap = AliyunUtils.sendSms(sendSmsDto);
                if ("OK".equals(hashMap.get("code"))) {
                    return true;
                }else{
                    return false;
                }
            } catch (ClientException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //腾讯云
        else if ("2".equals(pushType)) {
            TencentUtil tencentUtil = new TencentUtil();
            String result = TencentUtil.sendSMS(telephone, code, appid, appKey, templateId, appSecret);
            if(result.equals("success")){
//                return "发送成功！";
            }else{
//                return "发送失败！";
            }
        }
        else if ("3".equals(pushType)) {
            JGUtil jGUtil=new JGUtil();
            try {
                JGUtil.createDeivceSms(telephone,"","",templateId,appKey,appSecret);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        //华为发送短信
        else if ("4".equals(pushType)) {
            JGUtil jGUtil=new JGUtil();
            try {
                RedisCache redisCache=new RedisCache();
                String phone="";
                HuaWeiSendSms.sendSMSHuaWei(redisCache,phone);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return  true;
    }

}
